package cn.b.sky.mes.mm.service.impl;

import cn.b.sky.mes.mm.dao.PullTypeDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.PullType;
import cn.b.sky.mes.mm.model.condition.PullTypeCondition;
import cn.b.sky.mes.mm.service.PullTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.Page;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * 
 * <strong>Title : PullTypeServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.service.impl<br></strong>
 * <strong>Description : </strong>物料拉动类型业务实现类<br> 
 * <strong>Create on : 2016年9月8日 下午1:41:58<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("pullTypeService")
public class PullTypeServiceImpl implements PullTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(PullTypeServiceImpl.class);

  @Autowired
  private PullTypeDao pullTypeDao;

  @Override
  public Page getPullTypesByCondition(PullTypeCondition pullingTypeCondition) {
    if (!StringUtils.isEmpty(pullingTypeCondition.getSiteCode())) {
      pullingTypeCondition.setData(pullTypeDao.getPullTypeListByCondition(pullingTypeCondition));
      pullingTypeCondition.setTotalCount(pullTypeDao.getPullTypeCountByCondition(pullingTypeCondition));
      return pullingTypeCondition;
    }
    return null;
  }

  @Override
  public PullType getPullTypeByCode(String siteCode, String pullingTypeCode) {
    return pullTypeDao.getPullTypeByCode(siteCode, pullingTypeCode);
  }

  @Override
  public void savePullType(PullType pullingType) {
    if (null != getPullTypeByCode(pullingType.getSiteCode(), pullingType.getPullTypeCode())) {
      pullTypeDao.updatePullType(pullingType);
    } else {
      pullTypeDao.insertPullType(pullingType);
    }
  }

  @Override
  public BatchJobReport deletePullTypeByCode(String siteCode, String pullingTypeCode) {
    if (!StringUtils.isEmpty(pullingTypeCode)) {
      String message = pullingTypeCode;
      String cause = "";
      boolean success = true;
      try {
        pullTypeDao.deletePullType(siteCode, pullingTypeCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "pullingTypeCode is null", false);
    }
  }

}
